#pragma once
#include <iostream>
#include <ctemplate/template.h>
#include <vector>
#include"../comm/Util.hpp"
#include"../comm/Log.hpp"
#include "oj_model_2.hpp"
namespace ns_view
{
    using namespace ns_log;
    using namespace ns_model;
    const std::string template_path = "./template_html/";
    class View
    {
    public:
        View() {}
        ~View() {}

    public:
        void AllExpandHtml(const std::vector<Question> &questions, std::string *html)
        {
            LOG(INFO)<<"题目数量"<<questions.size()<<"\n";
            // 构建网址路劲
            const std::string src_html = template_path + "all_questions.html";
            // 形成数据字典
            ctemplate::TemplateDictionary root("all_questions");
            // 添加数据到数据字典
            for (const auto &q : questions)
            {
                ctemplate::TemplateDictionary* sub=root.AddSectionDictionary("question_list");
                sub->SetValue("number",q.number);
                sub->SetValue("title",q.title);
                sub->SetValue("star",q.star);
            }
            //获取要被渲染的html
            ctemplate::Template* tlp=ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);
            //开始完成渲染
            tlp->Expand(html,&root);
        }
        void OneExpandHtml(const Question& q,std::string* html)
        {
            //构建路劲
            const std::string src_html=template_path+"one_question.html";
            //形成数据字典
            ctemplate::TemplateDictionary root("one_question");
            //构建key和value值
            root.SetValue("number",q.number);
            root.SetValue("title",q.title);
            root.SetValue("star",q.star);
            root.SetValue("desc",q.desc);
            root.SetValue("pre_code",q.header);
            //获取要被渲染的html
            ctemplate::Template* tlp=ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);

            //开始完成渲染
            tlp->Expand(html,&root);
        }
    };
}